<?php
/**
 * Student Kinfolk Controller
 *
 * Student Kinfolk Information Management
 * student entry
 *
 * @version $Id: KinfolkController.php 93 2010-05-02 03:33:24Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Controller_Action
 * @package student
 * @subpackage Controller
 */
class Student_KinfolkController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = dirname(__FILE__)."/../languages/$locale/data.ini";
        $ini = new Zend_Config_Ini($path);
        $arrayData = $ini->get('array_data')->toArray();
        $this->view->callingArr = $arrayData['relation'];

        $this->view->callingJSON = Zend_Json::encode($arrayData['relation']);
    }

    public function indexAction()
    {
        if ($this->getRequest()->getParam('year_semester')){
       	    $this->view->year_semester = $this->getRequest()->getParam('year_semester');
       	    $this->view->class_id = $this->getRequest()->getParam('class_id');
       	}
       	else{
       	    $cookie = Zend_Controller_Request_Http::getCookie();
       	    if ( isset($cookie['Student_year_semester'])) {
       	        $this->view->year_semester = $cookie['Student_year_semester'];
       	        $this->view->class_id = $cookie['Student_class_id'];
       	    }else {
       	        $this->view->year_semester = $this->view->Semester['year'].'-'.$this->view->Semester['semester'];
       	        $this->view->class_id = $this->view->lowYear.'-1';
       	    }
       	}
    }

    function getdataAction()
    {
        $this->getHelper('layout')->disableLayout();
        $sn = (int)$this->getRequest()->getParam('sn');
        $q = Doctrine_Query::create()
        ->from('Student_Model_StudentKinfolk')
        ->where('student_sn=?',$sn)
        ->orderby('calling');
        $res = $q->fetchArray();

        $this->view->data = $res;
    }

    function setAction()
    {
        $request = $this->getRequest();
        $student_sn = $request->getParam('student_sn');
        $name = $request->getParam('name');
        $calling = $request->getParam('calling');
        $phone_office = $request->getParam('phone_office');
        $phone_mobile = $request->getParam('phone_mobile');
        $email = $request->getParam('email');
        $website = $request->getParam('website');
        $kinfolk = new Student_Model_StudentKinfolk();
        $form = new Student_Form_StudentKinfolk();
        $data = array();
        $message = array();
        if ($name){
            foreach($name  as $sn=>$val) {
                unset($data);
                $data['name'] = $val;
                $data['calling'] = $calling[$sn];
                $data['phone_office'] = $phone_office[$sn];
                $data['phone_mobile'] = $phone_mobile[$sn];
                $data['email'] = $email[$sn];
                $data['website'] = $website[$sn];
                $data['student_sn'] = $student_sn;
                $data['sn'] = $sn;
                if ($form->isValid($data)){
                    $kinfolk->assignIdentifier($sn);
                    $kinfolk->fromArray($data);
                    $kinfolk->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }
        // new data
        $name = $request->getParam('new_name');
        $calling = $request->getParam('new_calling');
        $phone_office = $request->getParam('new_phone_office');
        $phone_mobile = $request->getParam('new_phone_mobile');
        $email = $request->getParam('new_email');
        $website = $request->getParam('new_website');
        if ($name) {
            foreach($name  as $sn=>$val) {
                unset($data);
                 $data['name'] = $val;
                $data['calling'] = $calling[$sn];
                $data['phone_office'] = $phone_office[$sn];
                $data['phone_mobile'] = $phone_mobile[$sn];
                $data['email'] = $email[$sn];
                $data['website'] = $website[$sn];
                $data['student_sn'] = $student_sn;
                //$data['sn'] = null;
                if ($form->isValid($data)){
                     $kinfolk = new Student_Model_StudentKinfolk();
                    $kinfolk->fromArray($data);
                    $kinfolk->save();
                }
                else
                $message[] = $form->getMessages();
            }
        }

        if ($message)
        $this->_helper->json(array('error'=>$message));
        else
        $this->_helper->json(array('success'=>1));
    }

  public function deleteAction()
    {
        $sn = (int)$this->getRequest()->getParam('sn',0);
          $q = Doctrine_Query::create()
        ->delete('Student_Model_StudentKinfolk')
        ->where('sn=?',$sn);
        $q->execute();
        $this->_helper->json(1);
    }
}

